package com.example.multithread.chapter01;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.stream.LongStream;

/**
 * @ClassName Example01
 * @Description TODO
 * @Author leejiliang
 * @DateTime 2020/4/27 2:06 PM
 * @Version 1.0
 */
public class ThreadExample extends Thread{
	public List<Long> result ;
//	public static final List<Long> result = new ArrayList<>();
	private Long begin;
	private Long end;

	public ThreadExample(Long begin, Long end, List<Long> resultContainer) {
		this.begin = begin;
		this.end = end;
		this.result = resultContainer;
	}

	private Logger logger = LoggerFactory.getLogger(ThreadExample.class);
	@Override
	public void run() {
		Long calculator = calculator(begin, end);
		result.add(calculator);
//		logger.info(Thread.currentThread().getName() +"计算结果: {}", calculator);
	}

	public Long calculator(Long begin, Long end) {
		return LongStream.rangeClosed(begin, end).boxed().reduce(Long::sum).orElse(0L);
	}
}
